package cn.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class Img2 {
    public static void main(String[] args) {
        // 彩色图像转黑白图像（灰度）
        try {
            //ImageIO.read()  彩色的
            BufferedImage src = ImageIO.read(new File("g1.jpg"));
            int width = src.getWidth();
            int height = src.getHeight();

            //黑白的  10 gray
            BufferedImage img = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
            Graphics2D g = img.createGraphics();

            //抗锯齿，使线条，文本平滑
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

            g.drawImage(src,0,0,null);


            Random rand = new Random();
            for (int i = 0; i < 20; i++) {
                g.setColor(new Color(255,255,0,rand.nextInt(20,100)));
                int size = rand.nextInt(10,90);
                int x = rand.nextInt(width);
                int y = rand.nextInt(height);
                g.fillOval(x,y,size,size);
            }

            g.dispose();

            //
            ImageIO.write(img,"jpg",new File("girl.jpg"));

            Runtime.getRuntime().exec("cmd /k start girl.jpg");



        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
